import com.study.util.FileUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * SearchRpcMethodContentTest
 *
 * @author wanghuilong
 * @version 1.0
 * @description
 * @date 2023/5/6 15:41
 */
public class SearchRpcMethodContentTest {


    static String notesRegex = "\\/\\*{2}\\s*([\\s\\S]*?)\\s*\\*\\/\\s*";

    static String methodRegex = "[a-zA-Z<>,0-9 ]+\\s+[a-zA-Z0-9_]+\\((\\s*)[a-zA-Z0-9_<> ,.@()]*\\)\\;";

    static String methodNameRegex = "[a-zA-Z<>,0-9 ]+\\s+[a-zA-Z0-9_]+\\(";

    static String filePath2 = "/Users/wanghuilong/IdeaProjects/hx-mall-service/client/src/main/java/com/jkys/hy/mall/client/rpc/order/OrderRefundSearchRpcService.java";

    public static void main(String[] args) {
        List<String> allLineList = FileUtil.readTxtFileIntoStringArrList("/Users/wanghuilong/IdeaProjects/lazy/src/main/resources/file/SearchRpcMethodContentTest.txt",null);

        List<String> serviceNames = new ArrayList<String>();

        for (String line : allLineList) {
            String[] split = line.trim().split(":");
            serviceNames.add(split[0]+":"+split[1]);
        }


        String dir = "/Users/wanghuilong/IdeaProjects/hx-mall-service/client/src/main/java/com/jkys/hy/mall/client/rpc";

        String systemName = "hx-mall-service.";

        List<File> fileList = FileUtil.getAllFile(new File(dir));

        Map<String,String> map = new HashMap<>();

        String regex = "(\\/\\*{2}\\s*([\\s\\S]*?)\\s*\\*\\/\\s*)?(^[ ]*@[a-zA-Z]+(\\([\\{\\}:\\\"$:\\.a-zA-Z0-9]+\\))*\\n*)?([a-zA-Z<>,0-9 ]+\\s+[a-zA-Z0-9_]+\\((\\s*)[a-zA-Z0-9_<> ,.@()]*\\)\\;)";

        //1 [ ]*(\\/\\*([^\\*^\\/]*|[\\*^\\/*]*|[^\\**\\/]*)*\\*\\/\\n*)*(^[ ]*@[a-zA-Z]+(\([\{\}:\"$:\.a-zA-Z0-9]+\))*)*\n*[a-zA-Z<>,0-9 ]+\(\n*([ ]*@[a-zA-Z]+[ ]*)*([a-zA-Z<>, ]+)*\);
        //2 ([ ]*\/\*([^\*^\/]*|[\*^\/*]*|[^\**\/]*)*\*\/){0,1}(^[ ]*@[a-zA-Z]+(\([\{\}:\"$:\.a-zA-Z0-9]+\))*)*\n*[a-zA-Z<>,0-9 ]+\(\n*([ ]*@[a-zA-Z]+[ ]*)*([a-zA-Z<>, \n]+)*\);

        Pattern pattern = Pattern.compile(regex);

        for (File file : fileList) {
            String fileContent = FileUtil.read(file.getAbsolutePath()).trim();

            String serviceInfo = matche(fileContent, "@Service.*");
            if(StringUtils.isBlank(serviceInfo)) {
                continue;
            }
            String serviceName = serviceInfo.substring(serviceInfo.indexOf("@Service(\"")+"@Service(\"".length(),serviceInfo.indexOf("\")")).replace(systemName,"");
            if(Boolean.FALSE.equals(serviceNames.contains(serviceName))) {
                continue;
            }

            Matcher matcher = pattern.matcher(fileContent);
            List<String> list = new ArrayList<>();
            while(matcher.find()){
                boolean key = true;
                String e=matcher.group(0);
                if(e.contains("{")) {
                    e = e.split("\\{")[1];
                }
                if(StringUtils.countMatches(e,";") > 1) {
                    key = false;
                    String[] split = e.split(";");
                    for (String s :split) {
                        s = s.trim();
                        if(StringUtils.isNotBlank(s)) {
                            list.add(s + ";");
                        }
                    }
                }
                if(key) {
                    list.add(e);
                }
            }
            for (String s:list) {
                String notes = matche(s, notesRegex);
                String method = matche(s,methodNameRegex).trim().split(" ")[1].replace("(","");
                map.put(serviceName+":"+method,getMethodNote(notes).replace("\n",","));
            }
        }

        for (Map.Entry<String,String> entry:map.entrySet()) {
            System.out.println(entry.getKey() + ":"+entry.getValue());
        }

        for (String s : allLineList) {
            String note = map.get(s);
            if(null == note) {
                continue;
            }
            note = note.trim().replace(",,","").trim().replace(",,,","").trim().replace(",,,,","").trim();
            if(note.startsWith(",")) {
                note = note.substring(1).trim();
            }
            if(note.endsWith(",")) {
                note = note.substring(0,note.length() - 2).trim();
            }
            System.out.println(s+"\t"+note);
        }

    }


    private static String getMethodNote(String note) {
        if(null == note) {
            return "";
        }
        String str = removeNoteFormat(note);
        String[] split = str.split("\n");
        StringBuilder stringBuilder = new StringBuilder();
        for (String s:split) {
            s = s.trim();
            if(s.startsWith("@")) {
                continue;
            }
            stringBuilder.append(s).append("\n");
        }
        return stringBuilder.toString();
    }

    private static String removeNoteFormat(String note) {
        return note.replace("/**","").replace("*/","").replace("*","");
    }

    public static  String matche(String str,String regex) {
        System.out.println(str);
        System.out.println(regex);
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        if(matcher.find()) {
            return matcher.group(0);
        }
        return null;
    }


}
